package com.xhwl.centiotdata.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: luyi
 * @Description: 时间类型转换 String to Date
 * @Date: Create in 14:21 2020/5/18
 */
@Component
public class DefaultDateConvert implements Converter<String, Date> {

    private static final Logger log = LoggerFactory.getLogger(DefaultDateConvert.class);

    private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";

    @Override
    public Date convert(String value) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        try {
            return formatter.parse(value);
        } catch (ParseException e) {
            throw new RuntimeException("时间格式转换失败  "+value);
        }
    }
}
